Conditions | 2 |
Paths | 1 |
Total Lines | 135 |
Code Lines | 55 |
Lines | 42 |
Ratio | 31.11 % |
Changes | 10 | ||
Bugs | 6 | Features | 4 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | (function(f) { |
||
19 | })(function() { |
||
20 | var define, module, exports; |
||
21 | module = { exports: (exports = {}) }; |
||
22 | /*! |
||
23 | * cookiejs.js | v1.0.0 | cookiejs object for setting/getting/removing cookies |
||
24 | * Copyright (c) 2017 Eric Zieger (MIT license) |
||
25 | * https://github.com/theZieger/cookiejs.js/blob/master/LICENSE |
||
26 | */ |
||
27 | |||
28 | /** |
||
29 | * throwTypeError |
||
30 | * |
||
31 | * @param {String} sName |
||
32 | * @param {String} sType |
||
33 | * |
||
34 | * @throws {TypeError} |
||
35 | */ |
||
36 | var throwTypeError = function(sName, sType) { |
||
37 | throw new TypeError(sName + ' is not of type ' + sType); |
||
38 | }; |
||
39 | |||
40 | var cookiejs = { |
||
41 | global: this.document ? this.document : { cookie: '' }, |
||
42 | |||
43 | /** |
||
44 | * sets or overwrites a cookie |
||
45 | * |
||
46 | * @param {String} sCookieName - the name of the cookie you want to set |
||
47 | * @param {String} sValue - the value you want to set |
||
48 | * @param {String} oAttributes - options e.g. domain, path, expires |
||
49 | * |
||
50 | * @throws {TypeError} if argument sCookieName is empty or not a string |
||
51 | */ |
||
52 | set: function(sCookieName, sValue, oAttributes) { |
||
53 | var sAttributes = ''; |
||
54 | |||
55 | sValue = sValue || ''; |
||
56 | |||
57 | if (!sCookieName || typeof sCookieName !== 'string') { |
||
58 | throwTypeError('sCookieName', 'string'); |
||
59 | } |
||
60 | |||
61 | if (typeof sValue !== 'string') { |
||
62 | throwTypeError('sValue', 'string'); |
||
63 | } |
||
64 | |||
65 | if (oAttributes === undefined) { |
||
66 | sAttributes += '; path=/'; |
||
67 | } else { |
||
68 | if (typeof oAttributes !== 'object' || Array.isArray(oAttributes)) { |
||
69 | throwTypeError('oAttributes', 'object'); |
||
70 | } |
||
71 | |||
72 | Object.keys(oAttributes).forEach(function(sAttr) { |
||
73 | if (sAttr === 'secure') { |
||
74 | if (oAttributes[sAttr] === true || oAttributes[sAttr] === 'true') { |
||
75 | sAttributes += ';' + sAttr; |
||
76 | return; |
||
77 | } else { |
||
78 | throwTypeError(sAttr, 'boolean'); |
||
79 | } |
||
80 | } else if (typeof oAttributes[sAttr] !== 'string') { |
||
81 | throwTypeError(sAttr, 'string'); |
||
82 | } |
||
83 | |||
84 | sAttributes += ';' + sAttr + '=' + oAttributes[sAttr]; |
||
85 | }); |
||
86 | } |
||
87 | |||
88 | cookiejs.global.cookie = |
||
89 | encodeURIComponent(sCookieName) + |
||
90 | '=' + |
||
91 | encodeURIComponent(sValue) + |
||
92 | sAttributes; |
||
93 | }, |
||
94 | |||
95 | /** |
||
96 | * returns the value of a cookie |
||
97 | * |
||
98 | * @param {String} sCookieName |
||
99 | * |
||
100 | * @throws {TypeError} |
||
101 | * |
||
102 | * @returns {String|Boolean} |
||
103 | */ |
||
104 | get: function(sCookieName) { |
||
105 | var gCookieValue = false; |
||
106 | |||
107 | if (!sCookieName || typeof sCookieName !== 'string') { |
||
108 | throwTypeError('sCookieName', 'string'); |
||
109 | } |
||
110 | |||
111 | cookiejs.global.cookie.split('; ').some(function(sCookie) { |
||
112 | var aCookie = sCookie.split('='); |
||
113 | |||
114 | if (decodeURIComponent(aCookie[0]) === sCookieName) { |
||
115 | gCookieValue = decodeURIComponent(aCookie[1]); |
||
116 | return true; |
||
117 | } |
||
118 | |||
119 | return false; |
||
120 | }); |
||
121 | |||
122 | return gCookieValue; |
||
123 | }, |
||
124 | |||
125 | /** |
||
126 | * removes a specific cookie |
||
127 | * |
||
128 | * oAttributes must contain the correct path and domain it won't |
||
129 | * remove the cookie |
||
130 | * |
||
131 | * @param {String} sCookieName |
||
132 | * @param {Object} oAttributes - options e.g. domain, path, expires |
||
133 | * |
||
134 | * @throws {TypeError} |
||
135 | */ |
||
136 | remove: function(sCookieName, oAttributes) { |
||
137 | var oRemoveAttributes = oAttributes || {}; |
||
138 | |||
139 | if ( |
||
140 | typeof oRemoveAttributes === 'object' && |
||
141 | !Array.isArray(oRemoveAttributes) |
||
142 | ) { |
||
143 | oRemoveAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT'; |
||
144 | } |
||
145 | |||
146 | cookiejs.set(sCookieName, '', oRemoveAttributes); |
||
147 | } |
||
148 | }; |
||
149 | |||
150 | module.exports = cookiejs; |
||
151 | |||
152 | return module.exports; |
||
153 | }); |
||
154 |